//
//  YWGestureNavigationControllere.m
//  YueshengToutiao
//
//  Created by yellow on 2021/1/7.
//  Copyright © 2021 YW. All rights reserved.
//

#import "YWGestureNavigationController.h"

@interface YWGestureNavigationController ()

@property(nonatomic,strong)NSMutableArray*images;

@property(nonatomic,strong)UIImageView*screenView;

@property(nonatomic,strong)UIView*coverView;


@end

@implementation YWGestureNavigationController

//网友1写的
#warning - 大致思路应该是这样，但是很多细节不太好

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interactivePopGestureRecognizer.delegate=nil;
    UIPanGestureRecognizer*gesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panBack:)];

    [self.view addGestureRecognizer:gesture];

    
}

-(void)viewDidAppear:(BOOL)animated
{

    if(self.images.count>0)return;

    [self screenSnapShot];

}

-(void)screenSnapShot
{

    UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO,1);

    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.view.layer renderInContext:context];

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

    [self.images addObject:image];

    UIGraphicsEndImageContext();

}

-(void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
{

    
    //push的时候隐藏tabbar
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    
    [super pushViewController:viewController animated:animated];
    
    [self screenSnapShot];
    

}

-(UIViewController*)popViewControllerAnimated:(BOOL)animated
{

    [self.images removeLastObject];

    return[super popViewControllerAnimated:animated];

}

-(void)panBack:(UIPanGestureRecognizer*)gesture
{

    CGFloat translationx = [gesture translationInView:self.view].x;

    if(translationx <0 || self.images.count<2) return;

    self.screenView.image = self.images[self.images.count-2];

    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    [window insertSubview:self.screenView atIndex:0];

    [window insertSubview:self.coverView aboveSubview:_screenView];

    gesture.view.transform = CGAffineTransformMakeTranslation(translationx,0);

    if(gesture.state == UIGestureRecognizerStateEnded|| gesture.state == UIGestureRecognizerStateCancelled) {

        
        gesture.view.transform = CGAffineTransformIdentity;
        
        if(translationx > self.view.frame.size.width * 0.5) {

            [self popViewControllerAnimated:NO];

            [self.screenView removeFromSuperview];

            [self.coverView removeFromSuperview];

        }

    }

}

-(NSMutableArray*)images
{
    if(!_images) {

    _images= [NSMutableArray array];
    }
    return _images;
}

-(UIImageView*)screenView
{
    if(!_screenView) {
        _screenView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        }
    return _screenView;
}

-(UIView*)coverView
{
    if(!_coverView) {
    _coverView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _coverView.backgroundColor= [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    }
    return _coverView;
}




@end
